package com.young.coderule.common.util;

import com.young.coderule.common.annotation.CodeRule;
import com.young.coderule.common.scan.ResourceDefinition;
import org.springframework.util.StringUtils;

/**
 * @DESCRIPTION: 工具类
 * @USER: Young
 * @DATE: 2022/7/29 22:21
 */
public class MyUtils {

    /**
        获取ResourceDefinition
     */
    public static ResourceDefinition getResourceDefinition(CodeRule codeRuleAnnotation, String controllerName) {
        if (codeRuleAnnotation == null) {
            throw new IllegalArgumentException("CodeRule注解不能为空!");
        }
        String code = "";
        if (!StringUtils.hasLength(codeRuleAnnotation.code())) {
            int controllerIndex = controllerName.indexOf("Controller");
            if (controllerIndex == -1) {
                throw new IllegalArgumentException("controller class name is not illegal, it should end with Controller!");
            }
            code = controllerName.substring(0, controllerIndex);
        } else {
            code = codeRuleAnnotation.code();
        }
        String name = code;
        if (StringUtils.hasLength(codeRuleAnnotation.name())) {
            name = codeRuleAnnotation.name();
        }
        String codeField = codeRuleAnnotation.codeField();
        return new ResourceDefinition(code, name, codeField);

    }
}
